<template>
    <div>
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :span="7">
                        <a-form-model-item label="搜索内容">
                            <a-input v-model="queryParam.storeName" placeholder="请输入序号、姓名、手机号后四位、菜品、门店进行搜索" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="7">
                        <a-form-model-item label="商户">
                            <j-dict-select-tag v-model="queryParam.merchantId"
                                dict-code="HC_MERCHANT,STORE_NAME,ID,DEL_FLAG=0" placeholder="请选择商户"
                                @change="searchQuery" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="10">
                        <a-form-model-item label="付款时间">
                            <a-date-picker :default-value="getTime(queryParam.timeBegin)" :show-time="{ format: 'HH:mm' }"
                                format="YYYY-MM-DD HH:mm" placeholder="开始时间"
                                @change="(value, dateString) => onChange(value, dateString, 'timeBegin')" />
                            -
                            <a-date-picker :default-value="getTime(queryParam.timeEnd)" :show-time="{ format: 'HH:mm' }"
                                format="YYYY-MM-DD HH:mm" placeholder="结束时间"
                                @change="(value, dateString) => onChange(value, dateString, 'timeEnd')" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="5">
                        <a-form-model-item label="订单状态">
                            <a-select v-model="queryParam.state" :allowClear="true" @change="searchQuery">
                                <a-select-option value="PAY">已付款</a-select-option>
                                <a-select-option value="COMPLETED">已完成</a-select-option>
                                <a-select-option value="CANCELED">已取消</a-select-option>
                                <a-select-option value="REFUNDING">退款中</a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="6">
                        <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                            <a-button type="primary" @click="searchReset" icon="reload"
                                style="margin-left: 8px">重置</a-button>
                            <!-- <a-button type="primary" @click="$refs.notPayOrderModal.show()" icon="reload" style="margin-left: 8px">未支付订单</a-button> -->
                        </span>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="table-operator">
            <!--      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
            <a-button type="primary" icon="download" @click="handleExportXls('订单')">导出</a-button>
            <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
                <a-button type="primary" icon="import">导入</a-button>
            </a-upload>
            <a-button type="primary" @click="getLoseOrder">获取丢失订单</a-button>
            <a-button type="primary" @click="clearRepeatOrder">清除重复订单</a-button>
            <!-- 高级查询区域 -->
            <!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
            <!--      <a-dropdown v-if="selectedRowKeys.length > 0">-->
            <!--        <a-menu slot="overlay">-->
            <!--          <a-menu-item key="1" @click="batchDel">-->
            <!--            <a-icon type="delete"/>-->
            <!--            删除-->
            <!--          </a-menu-item>-->
            <!--        </a-menu>-->
            <!--        <a-button style="margin-left: 8px"> 批量操作-->
            <!--          <a-icon type="down"/>-->
            <!--        </a-button>-->
            <!--      </a-dropdown>-->
        </div>

        <!-- table区域-begin -->
        <div>
            <!--      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">-->
            <!--        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项-->
            <!--        <a style="margin-left: 24px" @click="onClearSelected">清空</a>-->
            <!--      </div>-->

            <a-table ref="table" size="middle" :scroll="{ x: true }" :bordered="false" rowKey="id" :columns="columns"
                :dataSource="dataSource" :pagination="ipagination" :loading="loading"
                :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" class="j-table-force-nowrap"
                @change="handleTableChange">
                <template slot="htmlSlot" slot-scope="text">
                    <div v-html="text"></div>
                </template>
                <template slot="imgSlot" slot-scope="text, record">
                    <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
                    <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
                        style="max-width: 80px; font-size: 12px; font-style: italic" />
                </template>
                <template slot="fileSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
                    <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                        下载
                    </a-button>
                </template>

                <span slot="action" slot-scope="text, record">
                    <!--  -->
                    <a-popconfirm v-if="!record.company && (record.state == '已付款' || record.state == '已完成')"
                        title="确定取消该订单吗?" @confirm="() => handleCancelOrder(record.id)">
                        <a>取消订单</a>
                    </a-popconfirm>
                    <!--          <a-divider type="vertical"/>-->
                    <!--          <a-dropdown>-->
                    <!--            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>-->
                    <!--            <a-menu slot="overlay">-->
                    <!--              <a-menu-item>-->
                    <!--                <a @click="handleDetail(record)">详情</a>-->
                    <!--              </a-menu-item>-->
                    <!--              <a-menu-item>-->
                    <!--                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">-->
                    <!--                  <a>删除</a>-->
                    <!--                </a-popconfirm>-->
                    <!--              </a-menu-item>-->
                    <!--            </a-menu>-->
                    <!--          </a-dropdown>-->
                </span>
            </a-table>
        </div>

        <hc-order-modal ref="modalForm" @ok="modalFormOk"></hc-order-modal>
        <!-- <NotPayOrderModal ref="notPayOrderModal"></NotPayOrderModal> -->
    </div>
</template>

<script>
import { getAction } from '@api/manage'
import moment from 'moment'

const renderContent = (value, row, index) => {
    const obj = {
        children: value,
        attrs: {},
    }
    if (index === 0) {
        obj.attrs.rowSpan = 2
    }
    return obj
}

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HcOrderModal from './HcOrderModal'
// import NotPayOrderModal from './modules/notPayOrderModal.vue'

function getDate(time) {
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let day = time.getDate()
    return { year, month, day }
}

let LastDate = new Date(new Date().getTime() - 1 * 1000 * 60 * 60 * 24)
let lastTime = getDate(LastDate)
let newDate = new Date()
let nowTime = getDate(newDate)
let timeBegin = `${lastTime.year}-${lastTime.month > 9 ? lastTime.month : '0' + lastTime.month}-${lastTime.day > 9 ? lastTime.day : '0' + lastTime.day
    } 17:00`
let timeEnd = `${nowTime.year}-${nowTime.month > 9 ? nowTime.month : '0' + nowTime.month}-${nowTime.day > 9 ? nowTime.day : '0' + nowTime.day
    } 12:00`

export default {
    name: 'HcOrderList',
    mixins: [JeecgListMixin, mixinDevice],
    components: {
        HcOrderModal,
        // NotPayOrderModal
    },
    data() {
        return {
            description: '订单管理页面',
            // 表头
            columns: [
                {
                    title: '#',
                    dataIndex: '',
                    key: 'rowIndex',
                    width: 60,
                    align: 'center',
                    customRender: function (t, r, index) {
                        return parseInt(index) + 1
                    },
                },
                {
                    title: '取餐序号',
                    dataIndex: 'pickingSerialNo',
                    align: 'center',
                },
                {
                    title: '地址',
                    align: 'center',
                    dataIndex: 'supplyAddress',
                    // customRender: (value, row, index) => {
                    //   return {
                    //     children: value,
                    //     attrs: {rowSpan: row.rowspan},
                    //   };
                    // }
                },
                {
                    title: '姓名',
                    align: 'center',
                    dataIndex: 'nameTakingMeal',
                    // customRender: (value, row, index) => {
                    //   return {
                    //     children: value,
                    //     attrs: {rowSpan: row.rowspan},
                    //   };
                    // }
                },
                {
                    title: '手机',
                    align: 'center',
                    dataIndex: 'phoneTakingMeals',
                    // customRender: (value, row, index) => {
                    //   return {
                    //     children: value,
                    //     attrs: {rowSpan: row.rowspan},
                    //   };
                    // }
                },
                {
                    title: '门店名称',
                    align: 'center',
                    dataIndex: 'storeName',
                    // customRender: (value, row, index) => {
                    //   return {
                    //     children: value,
                    //     attrs: {rowSpan: row.rowspan2},
                    //   };
                    // }
                },
                {
                    title: '菜品名称',
                    align: 'center',
                    dataIndex: 'nameDishes',
                },
                {
                    title: '备注',
                    align: 'center',
                    dataIndex: 'remarks',
                },
                // {
                //   title: '份数',
                //   align: "center",
                //   dataIndex: 'commodityQuantity'
                // },
                {
                    title: '合作单价',
                    algin: 'center',
                    dataIndex: 'commodityPrice',
                },
                {
                    title: '消费金额',
                    align: 'center',
                    dataIndex: 'totalPrice',
                    // customRender: (value, row, index) => {
                    //   return {
                    //     children: value,
                    //     attrs: {rowSpan: row.rowspan},
                    //   };
                    // }
                },
                {
                    title: '抽佣金额',
                    algin: 'center',
                    dataIndex: 'commission',
                },
                {
                    title: '付款时间',
                    align: 'center',
                    dataIndex: 'createTime',
                    // customRender: (value, row, index) => {
                    //   return {
                    //     children: value,
                    //     attrs: {rowSpan: row.rowspan},
                    //   };
                    // }
                },
                {
                    title: '下单累计次数',
                    align: 'center',
                    dataIndex: 'orderCount',
                    // customRender: (value, row, index) => {
                    //   return {
                    //     children: value,
                    //     attrs: {rowSpan: row.rowspan},
                    //   };
                    // }
                },
                {
                    title: '状态',
                    align: 'center',
                    dataIndex: 'state',
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    align: 'center',
                    width: 147,
                    scopedSlots: { customRender: 'action' },
                    // customRender: (value, row, index) => {
                    //   return {
                    //     // children: <a @click="handleEdit(record)" >取消订单</a>,
                    //     children: this.$createElement(
                    //         'a',
                    //         {
                    //           text: "123",
                    //           on: {
                    //             click: () => {
                    //               this.handleEdit(row)
                    //             }
                    //           },
                    //         },
                    //         ["取消订单"]
                    //     ),
                    //     attrs: {rowSpan: row.rowspan},
                    //   };
                    // }
                },
            ],
            url: {
                list: '/hc/hcOrderMerchant/list',
                delete: '/hc/hcOrder/delete',
                deleteBatch: '/hc/hcOrder/deleteBatch',
                exportXlsUrl: '/hc/hcOrderMerchant/platformExportXls',
                importExcelUrl: 'hc/hcOrderMerchant/importExcel',
            },
            dictOptions: {},
            superFieldList: [],
            queryParam: {
                timeBegin,
                timeEnd,
            },
        }
    },
    created() {
        this.getSuperFieldList()
    },
    computed: {
        importExcelUrl: function () {
            return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
        },
    },
    mounted() { },
    methods: {
        moment,
        getTime(time) {
            return time ? this.moment(time, 'YYYY-MM-DD HH:mm') : null
        },
        handleCancelOrder(id) {
            this.loading = true
            let that = this
            // getAction('/hc/hcOrderMerchant/cancelOrder', { id: id })
            getAction('hc/hcOrderMerchant/platformCancelOrder', { id: id })
                .then((res) => {
                    if (res.success) {
                        that.loadData()
                    } else {
                        that.$message.warning(res.message)
                    }
                })
                .finally(() => {
                    that.loading = false
                })
        },
        onChange(value, dateString, field) {
            this.$set(this.queryParam, field, dateString)
        },
        // loadData(arg) {
        //   if (!this.url.list) {
        //     this.$message.error("请设置url.list属性!")
        //     return
        //   }
        //   //加载数据 若传入参数1则加载第一页的内容
        //   if (arg === 1) {
        //     this.ipagination.current = 1;
        //   }
        //   var params = this.getQueryParams();//查询条件
        //   this.loading = true;
        //   getAction(this.url.list, params).then((res) => {
        //     if (res.success) {
        //       //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        //
        //       this.dataSource = [];
        //
        //       res.result.records.forEach((item, index) => {
        //         let datas = [];
        //         let rowspan = 0; //记录订单rowspan
        //
        //         let datas2 = [];
        //         let rowspan2 = 0; //记录商户rowspan
        //
        //         item.hcOrderDetailedList.forEach((item3, index3) => {
        //           rowspan++;
        //           rowspan2++;
        //
        //           item3.id = item.id;
        //           item3.supplyAddress = item.supplyAddress;
        //           item3.nameTakingMeal = item.nameTakingMeal;
        //           item3.phoneTakingMeals = item.phoneTakingMeals;
        //           item3.orderCount = item.orderCount;
        //           item3.totalPrice = item.totalPrice;
        //           item3.storeName = item.storeName;
        //           item3.createTime = item.createTime;
        //           item3.indexs = index + 1;
        //           item3.rowspan = 0;
        //           item3.rowspan2 = 0;
        //
        //           datas2.push(item3)
        //         })
        //
        //         datas2[0].rowspan = rowspan;
        //         datas2[0].rowspan2 = rowspan2;
        //         this.dataSource.push(...datas2);
        //
        //         // item.hcMerchants.forEach((item2, index2) => {
        //         //   let datas2 = [];
        //         //   let rowspan2 = 0; //记录商户rowspan
        //         //
        //         //   item2.hcOrderDetailedList.forEach((item3, index3) => {
        //         //     rowspan++;
        //         //     rowspan2++;
        //         //
        //         //     item3.id = item.id;
        //         //     item3.supplyAddress = item.supplyAddress;
        //         //     item3.nameTakingMeal = item.nameTakingMeal;
        //         //     item3.phoneTakingMeals = item.phoneTakingMeals;
        //         //     item3.totalPrice = item.totalPrice;
        //         //     item3.createTime = item.createTime;
        //         //     item3.indexs = index + 1;
        //         //     item3.rowspan = 0;
        //         //     item3.rowspan2 = 0;
        //         //
        //         //     datas2.push(item3)
        //         //   })
        //         //
        //         //   datas2[0].rowspan2 = rowspan2;
        //         //   datas.push(...datas2)
        //         //   console.log("this.datas2", datas2)
        //         //   console.log("this.datas", datas)
        //         // })
        //         //
        //         // datas[0].rowspan = rowspan;
        //         // this.dataSource.push(...datas);
        //       })
        //
        //       // console.log("this.dataSource", this.dataSource)
        //       // this.dataSource = res.result.records;
        //
        //       if (res.result.total) {
        //         this.ipagination.total = res.result.total;
        //       } else {
        //         this.ipagination.total = 0;
        //       }
        //       //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        //     } else {
        //       this.$message.warning(res.message)
        //     }
        //   }).finally(() => {
        //     this.loading = false
        //   })
        // },
        initDictConfig() { },
        getSuperFieldList() {
            let fieldList = []
            fieldList.push({ type: 'string', value: 'userId', text: '用户ID' })
            fieldList.push({ type: 'string', value: 'state', text: '状态' })
            fieldList.push({ type: 'int', value: 'pickingSerialNo', text: '取餐序号' })
            fieldList.push({ type: 'string', value: 'pickingAddress', text: '取餐地址' })
            fieldList.push({ type: 'number', value: 'totalPrice', text: '总价格' })
            this.superFieldList = fieldList
        },

        tabChange(e) {
            this.current = e;
        },

        //获取遗失订单
        async getLoseOrder(){
            let res=await getAction('/hc/hcOrder/reGetOrder');
            console.log('res',res);
            if(res.success){
                this.$message.success(res.result);
                this.loadData();
            }else{
                this.$message.warning(res.result);
            }
        },
        //清除重复订单
        async clearRepeatOrder(){
            let res=await getAction('/hc/hcOrder/clearRepeatOrder');
            console.log('res',res);
            if(res.success){
                this.$message.success(res.result);
                this.loadData();
            }else{
                this.$message.warning(res.result);
            }
        }
    },
}
</script>
<style scoped>
@import '~@assets/less/common.less';

/*/deep/ table tbody tr:hover > td {*/
/*  background-color: #ffffff !important*/
/*}*/
</style>